﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using System.Data;

namespace WebPresentation
{
    public partial class Document_Type : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserID"] == null)
                {
                    Response.Redirect("LoginPage.aspx");
                }
                if (!Session["Role"].Equals("Admin"))
                {
                    Response.Redirect("DocumentUser.aspx");
                }
            }       
        }

        DocTypeBO TypeBo = null;
        protected void ImageButtonadd_Click(object sender, ImageClickEventArgs e)
        {
            TypeBo = new DocTypeBO();
            string name = txtdoctypename.Text;
            string code = txtdoctypecode.Text;
            
            if (TypeBo.AddNewDocType(name, code) > 0)
            {
                Label10.Text = "Add successfully";
                dtgdoctype.DataBind();
            }
            else
            {
                Label10.Text = "Add failed";
            }
        }

        protected void dtgdoctype_SelectedIndexChanged(object sender, EventArgs e)
        {
            DocTypeBO Type = new DocTypeBO();
            DataTable dt = (DataTable)Type.GetByID(Int32.Parse(dtgdoctype.SelectedRow.Cells[1].Text));
            txtNameEdit.Text = dt.Rows[0]["dtName"].ToString();
            txtCodeEdit.Text = dt.Rows[0]["dtCode"].ToString();
        }

        protected void ImageButtonEdit_Click(object sender, ImageClickEventArgs e)
        {
            if (dtgdoctype.SelectedRow != null)
            { 
                TypeBo = new DocTypeBO();
                string name = txtNameEdit.Text;
                string code = txtCodeEdit.Text;
                int id = Int32.Parse(dtgdoctype.SelectedRow.Cells[1].Text);

                if (TypeBo.UpdateDocTypes(name, code,id) > 0)
                {
                    Label10.Text = "Update successfully";
                    dtgdoctype.DataBind();
                }
                else
                {
                    Label10.Text = "Update failed";
                }
            }
        }

        protected void LinkButtondel_Click(object sender, EventArgs e)
        {
            
            if (dtgdoctype.SelectedRow != null)
            {
                TypeBo = new DocTypeBO();
                int id = Int32.Parse(dtgdoctype.SelectedRow.Cells[1].Text);

                try
                {
                    if (TypeBo.DeleteDocType(id) > 0)
                    {
                        Label10.Text = "Delete Successfully";
                    }
                    else
                    {
                        Label10.Text = "Delete failed!";
                    }
                }
                catch (System.Data.SqlClient.SqlException)
                {

                    Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", "<script> alert('Warning: Please delete all the data that refer to this record first!')</script>");
                }
                dtgdoctype.DataBind();
            }
        }

        protected void LinkButtonedit_Click(object sender, EventArgs e)
        {
            if (dtgdoctype.SelectedRow != null)
            {
                DocTypeBO Type = new DocTypeBO();
                DataTable dt = (DataTable)Type.GetByID(Int32.Parse(dtgdoctype.SelectedRow.Cells[1].Text));
                txtNameEdit.Text = dt.Rows[0]["dtName"].ToString();
                txtCodeEdit.Text = dt.Rows[0]["dtCode"].ToString();
            }
            else
            {
            
            }
        }
    }
}